import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * roleDataAuthorityLovView - 租户级权限维护tab页 - 值集 model
 * @date: 2019-6-19

 */
import { createPagination, getResponse } from 'utils/utils';
import { deleteData, queryData, queryDataSourceModalData, saveData } from "../../services/roleDataAuthorityService";
export default {
  namespace: 'roleDataAuthorityDataSource',
  state: {
    head: {},
    // 头部数据
    list: [],
    // 请求查询到的数据
    pagination: {},
    // 分页信息
    createDataSource: [],
    createPagination: {}
  },
  effects: {
    fetchAuthorityDataSource: /*#__PURE__*/_regeneratorRuntime.mark(function fetchAuthorityDataSource(_ref, _ref2) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function fetchAuthorityDataSource$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(queryData, payload);

            case 4:
              response = _context.sent;
              data = getResponse(response);

              if (!data) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  head: data.roleAuthData,
                  list: data.roleAuthDataLineList.content,
                  pagination: createPagination(data.roleAuthDataLineList)
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchAuthorityDataSource);
    }),
    addAuthorityDataSource: /*#__PURE__*/_regeneratorRuntime.mark(function addAuthorityDataSource(_ref3, _ref4) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function addAuthorityDataSource$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(saveData, payload);

            case 4:
              response = _context2.sent;
              return _context2.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, addAuthorityDataSource);
    }),
    deleteAuthorityDataSource: /*#__PURE__*/_regeneratorRuntime.mark(function deleteAuthorityDataSource(_ref5, _ref6) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function deleteAuthorityDataSource$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(deleteData, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, deleteAuthorityDataSource);
    }),
    fetchModalData: /*#__PURE__*/_regeneratorRuntime.mark(function fetchModalData(_ref7, _ref8) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function fetchModalData$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call, put = _ref8.put;
              _context4.next = 4;
              return call(queryDataSourceModalData, payload);

            case 4:
              response = _context4.sent;
              data = getResponse(response);

              if (!data) {
                _context4.next = 9;
                break;
              }

              _context4.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  createDataSource: data.content,
                  createPagination: createPagination(data)
                }
              });

            case 9:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchModalData);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref9) {
      var payload = _ref9.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};